package com.sunshine.serialport.view;

import com.sunshine.serialport.entity.SerialData;
import com.sunshine.serialport.service.SerialDataService;
import com.sunshine.serialport.tools.BeanInfoUtil;
import com.sunshine.serialport.tools.query.QueryDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author:xiayingjie
 * @createTime:2021-04-01
 */
@RestController
@RequestMapping(value = "serial")
public class SerialController extends BaseController{

    @Autowired
    SerialDataService serialDataService;

    @RequestMapping(value = "list")
    public JsonResult list(HttpServletRequest request, QueryDTO queryDTO) {
        queryDTO.setOrder("-createAt");

        Page<SerialData> pageInfo=this.search(request,queryDTO,serialDataService, SerialData.class);

        List dataList= BeanInfoUtil.list2map(pageInfo.getContent());

        Map extraMap=this.getPageMap(pageInfo,queryDTO);
        return new JsonResultExtra(ResultCode.SUCCESS,dataList,extraMap);

    }
    @RequestMapping(value = "del")
    public JsonResult del(HttpServletRequest request, int id) {
        serialDataService.deleteById(id);
        return new JsonResult(ResultCode.SUCCESS);
    }


    @RequestMapping(value = "save")
    public JsonResult save(HttpServletRequest request, SerialData serialData) {
        SerialData serialData1=serialDataService.save(serialData);
        return new JsonResult(ResultCode.SUCCESS, BeanInfoUtil.bean2map(serialData1));
    }
}
